#!/usr/bin/env ruby

LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.realpath($PROGRAM_NAME)))

require "#{LKP_SRC}/lib/string_ext"
require "#{LKP_SRC}/lib/tests/stats"

stats = LKP::Stats.new

is_divided = false

while (line = $stdin.gets)
  line = line.resolve_invalid_bytes

  case line
  when /test_bpf: #[0-9]+ (.+) jited:.+ PASS$/
    type = $1.tr(' ', '_')
    stats.add_test_result line, type, 'pass' unless stats.key? type.to_s
  when /test_bpf: #[0-9]+ (.+) jited:.+ FAIL/
    type = $1.tr(' ', '_')
    stats.add_test_result line, type, 'fail' unless stats.key? type.to_s
  when /test_bpf: #[0-9]+ check: (.+) PASS$/
    type = $1.tr(' ', '_')
    stats.add_test_result line, "check:_#{type}", 'pass'
  when /test_bpf: #[0-9]+ check: (.+) FAIL/
    type = $1.tr(' ', '_')
    stats.add_test_result line, "check:_#{type}", 'fail'
  when /test_bpf: #[0-9]+ (.+) PASS$/
    type = $1.tr(' ', '_')
    stats.add_test_result line, type, 'pass'
  when /test_bpf: #[0-9]+ (.+) FAIL/
    type = $1.tr(' ', '_')
    stats.add_test_result line, type, 'fail'
  when /test_bpf: #[0-9]+ check: (.+)/
    type = "check:_#{$1.strip.tr(' ', '_')}"
    is_divided = true
  when /test_bpf: #[0-9]+ (.+)/
    type = $1.strip.tr(' ', '_')
    is_divided = true
  when /(PASS|FAIL)/
    if is_divided
      stats.add_test_result line, type, $1.downcase
      is_divided = false
    end
  end
end

stats.dump
